package com.yz.mcd.view;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.yz.mcd.MainActivity;
import com.yz.mcd.R;
import com.yz.mcd.view.impl.HomePager;
import com.yz.mcd.view.impl.MePager;
import com.yz.mcd.view.impl.PatternPager;
import com.yz.mcd.view.impl.RoomPager;

import java.util.ArrayList;

/**
 * 主页面fragment
 *
 * Created by Maverick on 2017/4/26.
 */

public class ContentFragment extends BaseFragment{

    private NoScrollViewPager mViewPager;

    private ArrayList<BasePager> mPagers;//五个标签页的集合

    private LinearLayout llHome,llRoom,llPattern,llMe;
    private ImageView ivHome,ivRoom,ivPattern,ivMe,ivCurrent;
    private TextView tvHome,tvRoom,tvPattern,tvMe,tvCurrent;

    @Override
    public View initView() {
        View view = View.inflate(mActivity, R.layout.fragment_content,null);
        mViewPager = (NoScrollViewPager) view.findViewById(R.id.vp_content);

        llHome = (LinearLayout) view.findViewById(R.id.ll_home);
        llRoom = (LinearLayout) view.findViewById(R.id.ll_room);
        llPattern = (LinearLayout) view.findViewById(R.id.ll_pattern);
        llMe = (LinearLayout) view.findViewById(R.id.ll_me);

        ivHome = (ImageView) view.findViewById(R.id.iv_home);
        ivRoom = (ImageView) view.findViewById(R.id.iv_room);
        ivPattern = (ImageView) view.findViewById(R.id.iv_pattern);
        ivMe = (ImageView) view.findViewById(R.id.iv_me);

        tvHome = (TextView) view.findViewById(R.id.tv_home);
        tvRoom = (TextView) view.findViewById(R.id.tv_room);
        tvPattern = (TextView) view.findViewById(R.id.tv_pattern);
        tvMe = (TextView) view.findViewById(R.id.tv_me);

        ivHome.setSelected(true);
        tvHome.setSelected(true);
        ivCurrent = ivHome;
        tvCurrent = tvHome;

        return view;
    }

    @Override
    public void initData() {
        mPagers = new ArrayList<BasePager>();

        //添加四个标签页
        mPagers.add(new HomePager(mActivity));
        mPagers.add(new RoomPager(mActivity));
        mPagers.add(new PatternPager(mActivity));
        mPagers.add(new MePager(mActivity));

        mViewPager.setAdapter(new ContentAdapter());

        //底栏标签切换监听
        llHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeTab(view.getId());
            }
        });
        llRoom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeTab(view.getId());
            }
        });
        llPattern.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeTab(view.getId());
            }
        });
        llMe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeTab(view.getId());
            }
        });
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                BasePager pager = mPagers.get(position);
                pager.initData();

                if (position == mPagers.size()-1){
                    //"我的"禁用侧边栏
                    setSlidingMenuEnable(false);
                }else{
                    //其他页面开启侧边栏
                    setSlidingMenuEnable(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //手动加载第一页数据
        mPagers.get(0).initData();

    }


    /**
     * 开启或禁用侧边栏
     *
     * @param enable
     */
    private void setSlidingMenuEnable(boolean enable) {
        //获取侧边栏对象
        Ac_main mainUI = (Ac_main) mActivity;
        SlidingMenu slidingMenu = mainUI.getSlidingMenu();
        if (enable){
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        }else {
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        }
    }

    class ContentAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mPagers.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            BasePager pager = mPagers.get(position);
            View view = pager.mRootView;//获取当前页面对象的布局

//            pager.initData();//初始化数据,viewpager会默认加载下一个页面，为了节省流量和性能，不要在此处调用初始化数据的方法

            container.addView(view);

            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    private void changeTab(int id){
        ivCurrent.setSelected(false);
        tvCurrent.setSelected(false);
        switch (id){
            case R.id.ll_home:
                mViewPager.setCurrentItem(0,false);//参2：表示是否具有滑动动画
//                mPagers.get(0).initData();
            case 0:
                ivHome.setSelected(true);
                ivCurrent = ivHome;
                tvHome.setSelected(true);
                tvCurrent = tvHome;
                break;
            case R.id.ll_room:
                mViewPager.setCurrentItem(1,false);
            case 1:
                ivRoom.setSelected(true);
                ivCurrent = ivRoom;
                tvRoom.setSelected(true);
                tvCurrent = tvRoom;
                break;
            case R.id.ll_pattern:
                mViewPager.setCurrentItem(2,false);
            case 2:
                ivPattern.setSelected(true);
                ivCurrent = ivPattern;
                tvPattern.setSelected(true);
                tvCurrent = tvPattern;
                break;
            case R.id.ll_me:
                mViewPager.setCurrentItem(3,false);
            case 3:
                ivMe.setSelected(true);
                ivCurrent = ivMe;
                tvMe.setSelected(true);
                tvCurrent = tvMe;
                break;
            default:
                break;
        }
    }
}
